package com.zhiyou.service.HouseServiceImpl;

import com.zhiyou.bean.House;
import com.zhiyou.dao.HouseMapper;
import com.zhiyou.service.HouseService.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    HouseMapper houseMapper;


    @Override
    public List<House> getAllHose() {
        Example example = new Example(House.class);
        Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("hStatus",4);
        return houseMapper.selectByExample(example);
    }

    @Override
    public void delHouse(String h_id) {
        //假删除所以把h_status 改成4 表示 假删除
        House house = new House();
        house.sethStatus(4);
        house.sethId(Integer.valueOf(h_id));
        int i = houseMapper.updateByPrimaryKeySelective(house);

        System.out.println("i = " + i);
    }

    @Override
    public House getOne(String h_id) {

        return houseMapper.selectByPrimaryKey(h_id);
    }

    @Override
    public List<House> searchHouse(String field, String keyword) {
        Example example = new Example(House.class);
        Criteria criteria = example.createCriteria();

        criteria.andLike(field, "%"+keyword+"%");
        criteria.andNotEqualTo("hStatus", 4);
        return houseMapper.selectByExample(example);
    }

    @Override
    public void updateHouse(House house, String updatatime) {
        DateFormat format1;
        Date date;
        format1=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format1.parse(updatatime);
            house.sethUpdatetime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        houseMapper.updateByPrimaryKeySelective(house);
    }

    @Override
    public void insertHouse(House house, String addTime, String updateTime) {
        DateFormat format1;
        Date date;
        Date date1;
        format1=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format1.parse(updateTime);
            date1=format1.parse(addTime);
            house.sethUpdatetime(date);
            house.sethAddtime(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        houseMapper.insert(house);
    }


}
